/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.wx.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.common.refun.CollectionUtil;
import com.jeesite.modules.common.refun.StringUtil;
import com.jeesite.modules.wx.entity.*;
import com.jeesite.modules.wx.service.WxActivityService;
import com.jeesite.modules.wx.service.WxBusinessUserService;
import com.jeesite.modules.wx.service.WxUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.wx.service.WxBusinessUserTongjiService;

import java.util.List;

/**
 * wx_business_user_tongjiController
 * @author wangjianxin
 * @version 2018-11-15
 */
@Controller
@RequestMapping(value = "${adminPath}/wx/wxBusinessUserTongji")
@Slf4j
public class WxBusinessUserTongjiController extends BaseController {

	@Autowired
	private WxBusinessUserTongjiService wxBusinessUserTongjiService;
	@Autowired
	private WxUserService wxUserService;
	@Autowired
	private WxBusinessUserService wxBusinessUserService;
	@Autowired
	private WxActivityService wxActivityService;

	private String wxBusinessUserId;


	/**
	 * 获取数据
	 */
	@ModelAttribute
	public WxBusinessUserTongji get(String id, boolean isNewRecord) {
		return wxBusinessUserTongjiService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(WxBusinessUserTongji wxBusinessUserTongji, Model model) {
		//这里的id是员工
		model.addAttribute("wxBusinessUserTongji", wxBusinessUserTongji);
		model.addAttribute("wxBusinessUserId", wxBusinessUserTongji.getId());
		wxBusinessUserId = wxBusinessUserTongji.getId();
		return "modules/wx/wxBusinessUserTongjiList";
	}
	
	/**1046275044689469440
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<WxBusinessUserTongji> listData(WxBusinessUserTongji wxBusinessUserTongji, HttpServletRequest request, HttpServletResponse response,
											   @RequestParam(value = "paramWxBusinessUserId") String paramWxBusinessUserId) {
		//查询商家员工的id
		WxBusinessUser wxBusinessUser = new WxBusinessUser();
		if(StringUtil.isNotEmpty(paramWxBusinessUserId)){
			wxBusinessUser = wxBusinessUserService.get(paramWxBusinessUserId);
		}

		WxUser wxUser = wxUserService.get(wxBusinessUser.getUserId().toString());
		WxBusinessUserTongji p = new WxBusinessUserTongji();
		if(null != wxUser){
			p.setUserOpenId(wxUser.getWxOpenId());
		}
		Page<WxBusinessUserTongji> page = wxBusinessUserTongjiService.findPage(new Page<WxBusinessUserTongji>(request, response), p);
		for(WxBusinessUserTongji temp : page.getList()){

			Long activityId = temp.getActivityId();

			WxActivity wxActivity = wxActivityService.get(activityId.toString());
			temp.setActivityName(wxActivity.getTitle());
			temp.setNickName(wxUser.getNickName());
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequestMapping(value = "form")
	public String form(WxBusinessUserTongji wxBusinessUserTongji, Model model) {
		model.addAttribute("wxBusinessUserTongji", wxBusinessUserTongji);
		return "modules/wx/wxBusinessUserTongjiForm";
	}

	/**
	 * 保存wx_business_user_tongji
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated WxBusinessUserTongji wxBusinessUserTongji) {
		wxBusinessUserTongjiService.save(wxBusinessUserTongji);
		return renderResult(Global.TRUE, text("保存wx_business_user_tongji成功！"));
	}
	
	/**
	 * 删除wx_business_user_tongji
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(WxBusinessUserTongji wxBusinessUserTongji) {
		wxBusinessUserTongjiService.delete(wxBusinessUserTongji);
		return renderResult(Global.TRUE, text("删除wx_business_user_tongji成功！"));
	}

	/**
	 * 分享员工统计增加
	 * @return
	 */
	@RequestMapping("share")
	@ResponseBody
	public Integer share(String activityId,String userOpenId,String inviteOpenId){

		//这里设计的员工的游览量++
		if(StringUtil.isNotEmpty(inviteOpenId)){
			if(!inviteOpenId.equals(userOpenId)){
				WxUser wxUser = wxUserService.findByOpenId(inviteOpenId);
				if(null == wxUser){
					logger.info("检查员工分享用户未找到");
					return 98;
				}
				//查找他是否是员工
				WxBusinessUser wxBusinessUser = new WxBusinessUser();
				wxBusinessUser.setUserId(Long.valueOf(wxUser.getId()));
				List<WxBusinessUser> wxBusinessUserList = wxBusinessUserService.findList(wxBusinessUser);
				if(CollectionUtil.isEmpty(wxBusinessUserList)){
					logger.info("inviteOpenId不是员工");
					return 0;
				}

				//查看商家是否在活动当中
				WxBusinessUser checkWxBusinesseUser = wxBusinessUserList.get(0);
				Long businessId = checkWxBusinesseUser.getBusinessId();
				WxActivity wxActivity = wxActivityService.get(activityId);
				String activityBusiness = wxActivity.getBusiness();
				String activityBusinessApply = wxActivity.getBusinessApply();
				if(StringUtil.isNotEmpty(activityBusiness)){
					if(activityBusiness.contains(businessId.toString()) || activityBusinessApply.contains(businessId.toString())){

					}else{
						logger.info("商家不在这次活动中，员工分享++无效");
						return 99;
					}
				}

				//员工统计
				WxBusinessUserTongji wxBusinessUserTongji = new WxBusinessUserTongji();
				//活动
				wxBusinessUserTongji.setActivityId(Long.valueOf(activityId));
				//员工的openId
				wxBusinessUserTongji.setUserOpenId(inviteOpenId);
				List<WxBusinessUserTongji> wxBusinessUserTongjiList = wxBusinessUserTongjiService.findList(wxBusinessUserTongji);
				if(CollectionUtil.isNotEmpty(wxBusinessUserTongjiList)){
					WxBusinessUserTongji temp =  wxBusinessUserTongjiList.get(0);
					Long zhuanfaCount = temp.getZhuanfa();
					if(null == zhuanfaCount){
						zhuanfaCount = 0L;
					}
					temp.setZhuanfa(zhuanfaCount+1L);
					wxBusinessUserTongjiService.save(temp);
				}else{
					//员工的userId

					wxBusinessUserTongji.setUserId(Long.valueOf(wxUser.getId()));
					wxBusinessUserTongji.setZhuanfa(1L);
					wxBusinessUserTongji.setWxBusinessUserId(Long.valueOf(checkWxBusinesseUser.getId()));
					wxBusinessUserTongjiService.save(wxBusinessUserTongji);
				}

			}else{
				log.info("自己转发自己了");
			}
		}

		return 1;
	}
	
}